--[[
任务集市相关逻辑
任务id：100724642T2201311050001 

任务2 轻松升至40级；达到筑基修真境界
--]]
local Log = require("common.Log")
local Lang = require("common.Lang")
local TaskMarketExcel = require("excel.TaskMarket")
local TaskMarketHelp = require("tencentApi.TaskMarketHelp")
local MailLogicFW = require("mail.MailLogicFW")
local Grid = require("bag.Grid")
local RoleFFI = require("role.RoleFFI")

CAN_GET = 1
HAS_GET = 2
function getStatusByStep(status, step)
  return (math.floor(status/(10^step)))%10
end 
function setStatusByStep(human, step, status)
  local old = getStatusByStep(human.db.appContractIdStatus, step) * (10^step)
  human.db.appContractIdStatus = human.db.appContractIdStatus - old
  human.db.appContractIdStatus = human.db.appContractIdStatus + status*(10^step)
end

-- 安装游戏 接受appContractId
function onTaskMarketLogin(human, appContractId)
	if appContractId == "" then return end
	if human.account == "100001-F135915CC600849C6CA2A2599EF8E6B9" then --hardcode 测试号
	   TaskMarketHelp.informTaskCompleted(human, appContractId,1)
	   TaskMarketHelp.informTaskCompleted(human, appContractId,2)
	   TaskMarketHelp.informTaskCompleted(human, appContractId,3)
	end
		
	local myid = RoleFFI.ffi_string(human.db.appContractId)
	if myid ~= "" then
	   return
	end
	
	local valid = nil
	for i = 1, #TaskMarketExcel.taskMarket do
		if appContractId == TaskMarketExcel.taskMarket[i].contractID then
			valid = true
			break
		end 
	end
	if valid then
		human.db.appContractId = appContractId
		human.db.appContractIdStatus = 0
		setStatusByStep(human, 1, CAN_GET)
		TaskMarketHelp.informTaskCompleted(human, appContractId,1)
		print("task_market_login:", human.db.appContractIdStatus)
	end
end

-- 升级
function onTaskMarketLevelup(human)
	checkTaskFinish(human)
end

function humanOneMinCheck(human)
	if not human.pf_info or not human.pf_info.appContractId or human.pf_info.appContractId == "" then
		return
	end
  
	local status2 = getStatusByStep(human.db.appContractIdStatus, 2) 
	local status3 = getStatusByStep(human.db.appContractIdStatus, 3)
	if status2 ~= 0 and status3 ~= 0 then
		return
	end

	local cf = nil
	for i = 1, #TaskMarketExcel.taskMarket do
		if human.pf_info.appContractId  == TaskMarketExcel.taskMarket[i].contractID then
			cf = TaskMarketExcel.taskMarket[i]
			break
		end 
	end
	
	if cf == nil then return end

	if cf.zhandouli1 == 0 and cf.zhandouli2 == 0 then return end

	print("taskMarket finish yeah :",  status2, status3)
	
	if cf.zhandouli1 > 0 then
		if human.db.zhandouli >= cf.zhandouli1 and status2 == 0 then
			setStatusByStep(human, 2, CAN_GET)
			TaskMarketHelp.informTaskCompleted(human, human.pf_info.appContractId,2)
		end
	end
	
	if cf.zhandouli2 > 0 then
		if human.db.zhandouli >= cf.zhandouli2 and status3 == 0 then
			setStatusByStep(human, 3, CAN_GET)
			TaskMarketHelp.informTaskCompleted(human, human.pf_info.appContractId ,3)
		end
	end
	
	print("check:",  human.db.appContractIdStatus)
end

function checkTaskFinish(human)
	if not human.pf_info or not human.pf_info.appContractId or human.pf_info.appContractId == "" then
		return
	end
  
	local status2 = getStatusByStep(human.db.appContractIdStatus, 2) 
	local status3 = getStatusByStep(human.db.appContractIdStatus, 3)
	if status2 ~= 0 and status3 ~= 0 then
		return
	end

	local cf = nil
	for i = 1, #TaskMarketExcel.taskMarket do
		if human.pf_info.appContractId  == TaskMarketExcel.taskMarket[i].contractID then
			cf = TaskMarketExcel.taskMarket[i]
			break
		end 
	end
	
	if cf == nil then return end
	if cf.reachLv1 == 0 and cf.reachLv2 == 0 then return end

	print("taskMarket finish yeah :",  status2, status3)

	if cf.reachLv1 > 0 then
		if human.db.lv >= cf.reachLv1 and status2 == 0 then
			setStatusByStep(human, 2, CAN_GET)
			TaskMarketHelp.informTaskCompleted(human, human.pf_info.appContractId,2)
		end
	end
		
	if cf.reachLv2 > 0 then
		if human.db.lv >= cf.reachLv2 and status3 == 0 then
			setStatusByStep(human, 3, CAN_GET)
			TaskMarketHelp.informTaskCompleted(human, human.pf_info.appContractId ,3)
		end
	end
	
	print("check:",  human.db.appContractIdStatus)
end

function onTaskMarketDeliver(uuid, appContractId, itemID, itemCount)
	local grid = {}	
	local grids = {}
	grids[1] = Grid.create(grid, itemID, 1, true)
	MailLogicFW.addSystemMessage(uuid, Lang.TASK_MARKET_TITLE, Lang.TASK_MARKET_CONTENT, grids)
	
	print("taskMarket get reward")
  Log.write(Log.LOGID_OSS_TASK_MARKET, uuid, "", "", appContractId, 3)	
	
	return 0, "ok"
end
